package com.cloud.school.app.util;

import io.swagger.annotations.ApiModelProperty;
import com.github.pagehelper.Page;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;


public class Pager<T> implements Serializable {
    public List<T> getResultList() {
        return resultList;
    }

    public void setResultList(List<T> resultList) {
        this.resultList = resultList;
    }

    public Paginator getPaginator() {
        return paginator;
    }

    public void setPaginator(Paginator paginator) {
        this.paginator = paginator;
    }

    /**
     * 查询结果
     */
    @ApiModelProperty("查询结果")
    private List<T> resultList;

    /**
     * 分页数据
     */
    @ApiModelProperty("分页数据")
    private Paginator paginator;

    public static <T> Pager<T> build(List<T> list) {
        Pager<T> pager = new Pager();
        pager.setResultList(list);
        if (list instanceof Page) {
            Page page = (Page) list;
            Paginator paginator = new Paginator();
            paginator.setTotalSize(page.getTotal());
            paginator.setTotalPage(page.getPages());
            paginator.setPageSize(page.getPageSize());
            paginator.setCurrentPage(page.getPageNum());
            pager.setPaginator(paginator);
        } else if (list instanceof Collection) {
            Paginator paginator = new Paginator();
            paginator.setTotalSize(list.size() + 0l);
            paginator.setTotalPage(1);
            paginator.setPageSize(list.size());
            paginator.setCurrentPage(1);
            pager.setPaginator(paginator);
        }
        return pager;
    }
}

